/**
 * Copyright (C), 2015-2018, XXX有限公司
 * FileName: TalkServiceImpl
 * Author:   臧浩鹏
 * Date:     2018/8/7 8:06
 * Description: 评论表的实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.etc.service.impl;

import com.etc.common.ResponseCode;
import com.etc.common.ServerResponse;
import com.etc.dao.TalkMapper;
import com.etc.dao.UserMapper;
import com.etc.entity.Talk;
import com.etc.entity.User;
import com.etc.service.TalkService;
import com.etc.utils.DateTimeUtil;
import com.etc.vo.TalkVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈评论表的实现类〉
 *
 * @author 臧浩鹏
 * @create 2018/8/7
 * @since 1.0.0
 */
@Service
public class TalkServiceImpl implements TalkService {
    @Autowired
    private TalkMapper talkMapper;
    @Autowired
    private UserMapper userMapper;




    @Override
    public ServerResponse<Talk> selectByTid(String tid){
        if (tid!=null){

            Talk talk = talkMapper.selectByPrimaryKey(Integer.parseInt(tid));
            if (talk==null){
                return ServerResponse.createByErrorMessage("评论或已删除！");
            }
            return ServerResponse.createBySuccess(talk);
        }
        return ServerResponse.createByErrorMessage("请选择正确id！");
    }

    /**
     *
     * @Description:
     *
     * @auther: 臧浩鹏
     * @date: 9:45 2018/8/7
     * @param: [talk, fatherId, fatherTime]
     * @return: com.etc.common.ServerResponse
     *
     */
    @Override
    public ServerResponse<TalkVo> insertTalk(Talk talk, Integer fatherId, String fatherTime){
        int res = 0;
        if(talk.getTtopicid() == null|| StringUtils.isEmpty(fatherTime)){
            //说明是回复的文章
            talk.setTtopicid(0);
            talk.setFathertime(new Date());
            System.out.println(talk.getFathertime());
            res = talkMapper.insert(talk);
        }else {
            talk.setTtopicid(fatherId);
            talk.setFathertime(DateTimeUtil.strToDate(fatherTime));
            res = talkMapper.insert(talk);
        }
        if (res>0){
            Talk resV = talkMapper.selectByPrimaryKey(talk.getTid());
            User user = userMapper.selectByPrimaryKey(resV.gettUid());
            TalkVo talkVo = new TalkVo();
            if (user == null){
                return ServerResponse.createByErrorMessage(ResponseCode.NEED_LOGIN.getCode(),"游客不能回复！");
            }
            talkVo.setTid(resV.getTid());
            talkVo.setUname(user.getUname());
            talkVo.setUimg(user.getUimg());
            talkVo.setUid(user.getUid());
            talkVo.setTtalkinfo(resV.getTcontent());
            talkVo.setChildrens(new ArrayList());
            talkVo.setFromcreatetime(resV.getTcreatetime().getTime());
            talkVo.setTcreatetime(resV.getTcreatetime().getTime());
            return ServerResponse.createBySuccess(talkVo);
        }else {
            return ServerResponse.createByErrorMessage("插入失败！");
        }
    }
    /**
     *
     * @Description: 控评 V2 版 解决V1的未递归删除的bug
     *
     * @auther: 臧浩鹏
     * @date: 8:40 2018/8/7
     * @param: [tid]
     * @return: com.etc.common.ServerResponse
     *
     */
    @Override
    public ServerResponse<Integer> deleteTalk(Integer tid){
        Talk talk = talkMapper.selectByPrimaryKey(tid);
        if(talk == null){
            return ServerResponse.createByErrorMessage("不存在该评论！评论或已删除!");
        }
        Integer before = talkMapper.selectCommentsNums(talk.getTcategory(), talk.getTarticleid());
        if (before == null){
            before = 0;
        }
        int res = talkMapper.deleteByPrimaryKey(talk.getTid());
        Date date = talk.getTcreatetime();
        System.out.println(res);
        ServerResponse<TalkVo> response = searchTalkAll(talk.getTcategory(), talk.getTarticleid(), talk.gettUid(), DateTimeUtil.dateToStr(date),false);
        List<TalkVo> talkVos = response.getData().getChildrens();
        System.out.println(talkVos+"++++++++++++++++++++++++++++++++++++");
        deleteWithDeep(talk,talkVos);
        Integer end = talkMapper.selectCommentsNums(talk.getTcategory(),talk.getTarticleid());
        if (end == null){
            end = 0;
        }
        int delCounts = before - end;
        System.out.println(delCounts);
        return ServerResponse.createBySuccess("删除评论成功,删除了"+delCounts+"条评论!",delCounts);
    }

    private void deleteWithDeep(Talk talk, List<TalkVo> talkVos){
        if (!(talkVos.size()>0)){
            return;
        }
        for (TalkVo vo:talkVos){
            Integer res = talkMapper.deleteTalkByDeep(talk.getTcategory(),talk.getTarticleid(), DateTimeUtil.dateToStr(new Date(vo.getTcreatetime())),vo.getUid());
            System.out.println(res);
            if (vo.getChildrens().size()>0){
                deleteWithDeep(talk,vo.getChildrens());
            }
        }
    }


    /**
     *
     * @Description: 查找文章全部一级评论时 uid = 0,createTime = null; 查看某人的所有评论时 要传递该人评论的createtime参数
     *               查找文章全部评论 all = true
     * @auther: 臧浩鹏
     * @date: 8:49 2018/8/7
     * @param: [tcategory, tarticleid, uid,createTime,all]
     * @return: com.etc.common.ServerResponse
     *
     */
    @Override
    public ServerResponse<TalkVo> searchTalkAll(String tcategory, Integer tarticleid, Integer uid, String createTime, Boolean all){

        TalkVo talkVo = new TalkVo();
        talkVo.setChildrens(new ArrayList());
        if (all == true){
            talkVo.setUname("root");
            talkVo.setUid(uid);
            //先拿到最先评论的几个评论者
            List<Talk> talks = talkMapper.searchOneLevelTalk(talkVo.getUid(), tcategory, tarticleid,createTime);
            this.readySearch(talks,talkVo,tcategory,tarticleid);
        }else if (uid != null&&uid != 0){
            talkVo.setUid(uid);
            User flag = userMapper.selectByPrimaryKey(uid);
            if (flag == null){
                return ServerResponse.createBySuccess("回复的评论不存在！");
            }
            talkVo.setUname(flag.getUname());
            talkVo.setUimg(flag.getUimg());
            //先拿到最先评论的几个评论者
            List<Talk> talks = talkMapper.searchOneLevelTalk(talkVo.getUid(), tcategory, tarticleid,createTime);
            this.readySearch(talks,talkVo,tcategory,tarticleid);
        }else {
            talkVo.setUname("root");
            talkVo.setUid(0);
            //先拿到最先评论的几个评论者
            List<Talk> talks = talkMapper.searchOneLevelTalk(talkVo.getUid(), tcategory, tarticleid,createTime);
            for(Talk talk : talks){
                User user = userMapper.selectByPrimaryKey(talk.gettUid());
                TalkVo temp = new TalkVo();
                temp.setTid(talk.getTid());
                temp.setUid(talk.gettUid());
                temp.setUname(user.getUname());
                temp.setUimg(user.getUimg());
                temp.setTcreatetime(talk.getTcreatetime().getTime());
                temp.setFromcreatetime(talk.getFathertime().getTime());
                temp.setTtalkinfo(talk.getTcontent());
                temp.setChildrens(new ArrayList());
                talkVo.getChildrens().add(temp);
            }
        }
        return ServerResponse.createBySuccess(talkVo);
    }

    @Override
    public Integer selectNumsByCidAndAid(String tcategory, Integer jid) {
        Integer res = talkMapper.selectCommentsNums(tcategory,jid);
        return res;
    }

    private void readySearch(List<Talk> talks, TalkVo talkVo, String tcategory, Integer tarticleid){

        for(Talk talk : talks){

            User user = userMapper.selectByPrimaryKey(talk.gettUid());
            TalkVo temp = new TalkVo();
            temp.setTid(talk.getTid());
            temp.setUid(talk.gettUid());
            temp.setUname(user.getUname());
            temp.setUimg(user.getUimg());
            temp.setTcreatetime(talk.getTcreatetime().getTime());
            temp.setFromcreatetime(talk.getFathertime().getTime());
            temp.setTtalkinfo(talk.getTcontent());
            temp.setChildrens(new ArrayList());
            talkVo.getChildrens().add(temp);
            this.searchTalkWithDeep(temp, tcategory, tarticleid, talk);
        }
    }
    public void searchTalkWithDeep(TalkVo talkVo, String tcategory, Integer tarticleid, Talk talk) {
        List<Talk> talks = talkMapper.selectByOwnWithTime(talk.gettUid(), DateTimeUtil.dateToStr(talk.getTcreatetime()), DateTimeUtil.dateToStr(talk.getFathertime()),tcategory,tarticleid);
        System.out.println(talks);
        for(Talk flag : talks){
            User user = userMapper.selectByPrimaryKey(flag.gettUid());
            TalkVo temp = new TalkVo();
            if (temp.getChildrens() == null){
                temp.setChildrens(new ArrayList());
            }
            temp.setTid(flag.getTid());
            temp.setUid(user.getUid());
            temp.setUname(user.getUname());
            temp.setUimg(user.getUimg());
            temp.setTtalkinfo(flag.getTcontent());
            temp.setTcreatetime(flag.getTcreatetime().getTime());
            temp.setFromcreatetime(flag.getFathertime().getTime());
            talkVo.getChildrens().add(temp);
            searchTalkWithDeep(temp,tcategory,tarticleid,flag);
        }
    }

}
